package com.basic.impl;

import java.util.Timer;
import java.util.TimerTask;

import com.basic.contant.NumsContant;
import com.basic.intf.IVersionLogic;

import android.content.Context;


public class VersionLogicImpl implements IVersionLogic{
	private static IVersionLogic instance = null;
	private Context mContext;
	private Timer mVersionUpdateTimer;
	
	private VersionLogicImpl(Context context) {
		
	}
	
	public static IVersionLogic getInstance(Context context) {
		if(instance == null) {
			instance = new VersionLogicImpl(context);
		}
		return instance;
	}
	
	@Override
	public boolean checkUpdated() {
		boolean result =false;//todo
		return result;
	}
	
	@Override
	public void setupAutoUpdateAsync() {
		long freq = NumsContant.freq;
		if(freq <= 0) {
			stopAutoUpdate();
			return;
		}
		TimerTask task = new TimerTask() {
			@Override public void run() {	
				doUpdate();
			}
		};
		long delayTime = 0;
		
		mVersionUpdateTimer = new Timer(true);
		mVersionUpdateTimer.schedule(task, delayTime, freq);
	}
	
	private void doUpdate() {
		long freq = NumsContant.freq;
		if(freq <= 0) {
			stopAutoUpdate();
			return;
		}
	}
	
	@Override
	public void stopAutoUpdate() {
		if(mVersionUpdateTimer != null) {
			mVersionUpdateTimer.cancel();
			mVersionUpdateTimer = null;
		}
	}
	
}
